package initialize

import (
	"fmt"
	_ "github.com/mbobakov/grpc-consul-resolver" // It's important  //负债均衡
	"google.golang.org/grpc"
	"service_base_shop_api/order_web/global"
	"service_base_shop_api/order_web/proto"
)

func InitUserSrvConn()  {
	//使用注册中心链接 grpc
	consulInfo := global.ServerConfig.ConsulInfo
	userconn,err := grpc.Dial(
		fmt.Sprintf("consul://%s:%d/%s?wait=14s", consulInfo.Host, consulInfo.Port, global.ServerConfig.UserSrvInfo.Name),
		grpc.WithInsecure(), //取消https 验证
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`), //负载均衡算法  pick_first 和 round_robin
		)
	if err != nil {
		panic(err)
	}
	global.UserSrvClient =  protopb.NewUserClient(userconn)
}

func InitJwtSrvConn()  {
	//使用注册中心链接 grpc
	consulInfo := global.ServerConfig.ConsulInfo
	userconn,err := grpc.Dial(
		fmt.Sprintf("consul://%s:%d/%s?wait=14s", consulInfo.Host, consulInfo.Port, global.ServerConfig.JwtSrvConfig.Name),
		grpc.WithInsecure(), //取消https 验证
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`), //负载均衡算法  pick_first 和 round_robin
	)
	if err != nil {
		panic(err)
	}
	global.JwtClient =  protopb.NewJwtClient(userconn)
}

func InitAddressSrvConn()  {
	//使用注册中心链接 grpc
	consulInfo := global.ServerConfig.ConsulInfo
	userconn,err := grpc.Dial(
		fmt.Sprintf("consul://%s:%d/%s?wait=14s", consulInfo.Host, consulInfo.Port, global.ServerConfig.AddressSrvConfig.Name),
		grpc.WithInsecure(), //取消https 验证
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`), //负载均衡算法  pick_first 和 round_robin
	)
	if err != nil {
		panic(err)
	}
	global.AddressClient =  protopb.NewAddrssClient(userconn)
}

func InitOrderSrvConn()  {
	//使用注册中心链接 grpc
	consulInfo := global.ServerConfig.ConsulInfo
	userconn,err := grpc.Dial(
		fmt.Sprintf("consul://%s:%d/%s?wait=14s", consulInfo.Host, consulInfo.Port, global.ServerConfig.OrderSrvConfig.Name),
		grpc.WithInsecure(), //取消https 验证
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`), //负载均衡算法  pick_first 和 round_robin
	)
	if err != nil {
		panic(err)
	}
	global.OrderClient =  protopb.NewOrderSrvClient(userconn)
}
